<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="展馆名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="展馆名称"></el-input>
      </el-form-item>
      <el-form-item label="展会id" prop="exhId">
        <el-input v-model="dataForm.exhId" placeholder="展会id"></el-input>
      </el-form-item>
      <el-form-item label="展馆说明" prop="title">
        <el-input v-model="dataForm.title" placeholder="展馆说明"></el-input>
      </el-form-item>
      <el-form-item label="展位图地址" prop="exhStandUrl">
        <el-image v-if="dataForm.exhStandUrl" :src="dataForm.exhStandUrl" fit="contain" :preview-src-list="[dataForm.exhStandUrl]" />
        <el-upload class="avatar-uploader" :action="url" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
          <el-button type="primary">选择文件</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="状态" prop="enable">
        <el-switch v-model="dataForm.enable" :active-value="1" :inactive-value="0" />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from "vue";
import { getToken } from "@/utils/cache";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
import app from "@/constants/app";
export default defineComponent({
  setup() {
    return reactive({
      url: "",
      visible: false,
      dataForm: {
        id: "",
        name: "",
        exhId: "",
        title: "",
        exhStandUrl: "",
        enable: ""
      }
    });
  },
  computed: {
    dataRule() {
      return {};
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.url = `${app.api}/sys/oss/upload?token=${getToken()}`;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    handleAvatarSuccess(response: any) {
      this.dataForm.exhStandUrl = response.data.src;
    },

    beforeAvatarUpload(rawFile: { type: string; size: number }) {
      if (rawFile.type !== "image/jpeg") {
        this.$message.error("必须是jpg格式文件!");
        return false;
      } else if (rawFile.size / 1024 / 1024 > 2) {
        this.$message.error("Avatar picture size can not exceed 2MB!");
        return false;
      }
      return true;
    },
    // 获取信息
    getInfo() {
      baseService.get("/noc/texhhall/" + this.dataForm.id).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = res.data;
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/noc/texhhall", this.dataForm).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    }
  }
});
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
